﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Agen.ServiceAgencyWPF;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Agen.Models;
namespace Agen.Views
{
    public partial class UserList : Page
    {
        AgencyServiceClient service;
        UserListVO vo = new UserListVO();
        public ObservableCollection<CustomerDC> CustomerList
        {
            get;
            set;
        }
        public UserList()
        {
            InitializeComponent();
            service = new AgencyServiceClient();
            CustomerList = new ObservableCollection<CustomerDC>();
            this.DataContext = vo;
            
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
             try {

                ApplicationManager.client.GetAllCustomerCompleted += new EventHandler<GetAllCustomerCompletedEventArgs>(client_GetAllCustomerCompleted);
                ApplicationManager.client.GetAllCustomerAsync();
            }catch(Exception ex){
                Debug.WriteLine(ex.Message);

            }


        }

        void client_GetAllCustomerCompleted(object sender, GetAllCustomerCompletedEventArgs e)
        {
            vo.CustomerList = e.Result;
        }
       

        private void search_Click(object sender, RoutedEventArgs e)
        {
            if (this.vo.CopyCustomerList == null)
            {
                this.vo.CopyCustomerList = this.vo.CustomerList;
            }

            if (this.vo.SearchName != "")
            {
                ObservableCollection<CustomerDC> temp = new ObservableCollection<CustomerDC>();
                foreach (CustomerDC customer in this.vo.CopyCustomerList)
                {
                    if (customer.Person.LastName.ToLower().Contains(this.vo.SearchName.ToLower()) || customer.Person.FirstName.ToLower().Contains(this.vo.SearchName.ToLower()))
                    {
                        temp.Add(customer);
                    }
                }
                this.vo.CustomerList = temp;
            }
            else
            {
                this.CustomerList = this.CustomerList;
            }
        }



        private void userList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Debug.WriteLine("DBG : USER_LIST => userList_SelectionChanged ");
            ApplicationManager.CURRENT_CUSTOMER = userList.SelectedItem as CustomerDC;
            ApplicationManager.FrameAction(this, ApplicationManager.CLICK_ON_CUSTOMER_HANDLE, new Object());
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("DBG : USER_LIST => add_Click ");
            ApplicationManager.FrameAction(this, ApplicationManager.CLICK_ON_ADD_CUSTOMER_HANDLE, new Object());
        }

        private void remove_Click(object sender, RoutedEventArgs e)
        {
            if(userList.SelectedItem != null){
                ApplicationManager.client.DeleteCustomerCompleted += new EventHandler<DeleteCustomerCompletedEventArgs>(client_DeleteCustomerCompleted);
                ApplicationManager.client.DeleteCustomerAsync((userList.SelectedItem as CustomerDC).IdCustomer);
            }else{
                MessageBox.Show("Select an element in the user list !"); 
            }
        }

        void client_DeleteCustomerCompleted(object sender, DeleteCustomerCompletedEventArgs e)
        {
            ObservableCollection<CustomerDC> copy = vo.CustomerList;
            if (e.Result != 0)
            {
                foreach (CustomerDC item in vo.CustomerList)
                {
                    if(item.IdCustomer == e.Result){
                        copy.Remove(item);
                        vo.CustomerList = copy;        
                    }
                }
            }
            else {
                MessageBox.Show("Erreur Supression !!");
            }
        }

        
    }

    public class UserListVO : INotifyPropertyChanged
    {
        private string _searchName;

        public string SearchName
        {
            get
            {
                return this._searchName;
            }
            set
            {
                this._searchName = value;


                
                OnPropertyChanged("SearchName");
            }
        }

        public UserListVO() {
            CustomerList = new ObservableCollection<CustomerDC>();
        }

        private ObservableCollection<CustomerDC> _customerList;

        public ObservableCollection<CustomerDC> CopyCustomerList{ get; set; }

        public ObservableCollection<CustomerDC> CustomerList
        {
            get
            {
                return this._customerList;
            }
            set
            {
                this._customerList = value;
                OnPropertyChanged("CustomerList");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }


}
